package com.geping.etl.utils;

import com.geping.etl.common.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Component
@Slf4j
public class EnvUitl implements InitializingBean {

    public static String profile;

    public static Properties props;

    public static String outSetting;

    public static String outSettingPath;

    public static String outSettingConfigName;


    @Value("${spring.profiles.active}")
    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Value("${out.setting}")
    public void setOutSetting(String outSetting) {
        this.outSetting = outSetting;
    }

    @Value("${out.setting.path}")
    public void setOutSettingPath(String outSettingPath) {
        this.outSettingPath = outSettingPath;
    }

    @Value("${out.setting.config.name}")
    public void setOutSettingConfigName(String outSettingConfigName) {
        this.outSettingConfigName = outSettingConfigName;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties properties = new Properties();
        try {
            //读取外置文件
            if(Constants.ON.equals(outSetting)){
                String path = outSettingPath + outSettingConfigName;
                FileInputStream ins = new FileInputStream(path);
                properties.load(ins);
            }else {
                properties.load(EnvUitl.class.getClassLoader().getResourceAsStream(EnvUitl.profile + "/config.properties"));
            }
            props = properties;
        } catch (IOException e) {
            log.error("config.properties error, e = {}", e);
        }
    }


}
